#ifndef HTMLGENERATOR_H_INCLUDED
#define HTMLGENERATOR_H_INCLUDED
#include <iostream>
#include <stdlib.h>
#include <string>
#include <stdio.h>
#include <fstream>
#include <time.h>
#include "../Core/scheduler.h"

namespace HtmlGenerator{
    void generate(Scheduler::CompleteSessionList cSessions, Scheduler::ClassroomList cList){
        int sessionSize = cSessions.size();
        int classroomSize = cList.size();
        std::vector<std::string> str, str2;

        //setup
        int **flags = new int*[classroomSize];
        for(int i = 0; i < classroomSize; i++){
            flags[i] = new int[Colors::colorCount];
            for(int j = 0; j < Colors::colorCount; j++)
                flags[i][j] = -1;
        }

        //setup the flags
        for(int c = 0; c < sessionSize; c++){
            int classroomIndex = cSessions[c].classroom.id() - 1;
            flags[classroomIndex][cSessions[c].session.allotedTime] = c;
        }
        system("mkdir schd");

        //writing the html files
        for(int y = 0; y < classroomSize; y++){
            Classroom *a = &cList[y];

           //write classroom hmtl
            std::string dir = "schd\\";
            dir.append(a->name());
            dir.append(".html");
            str.push_back(std::string(dir));
            str2.push_back(std::string(a->name()));
            std::ofstream out(dir.c_str(), std::ios::out);
            std::cout <<"Writing file: " << dir << std::endl;
            out << "<html>" << std::endl;
            out << "<header><h1><div align =\"center\">" << a->name() << "</div></h1></header>" << std::endl;
            out << "<body>" << std::endl;
            out << "<table border=\"1\">" << std::endl;
            out << "<tr><th>Time</th><th>Sessions</th></tr>" << std::endl;
            for(int z = 0; z < Colors::colorCount; z++){
                int index = flags[y][z];
                if(index == -1)
                    continue;
                out << "<tr>" << std::endl;

                out << "<td>";
                out << Colors::toString(cSessions[index].session.allotedTime);
                out << "</td>" << std::endl;

                out << "<td>";
                out << "<p>Subject: " << cSessions[index].session.subject.code() << "</p>" << std::endl;
                out << "<p>Group: " << cSessions[index].session.group.name() << "</p>" << std::endl;
                out << "</td>" << std::endl;

                out << "</tr>" << std::endl;
            }
            out << "</table>" << std::endl;
            out << "<div align=\"left\">";
            out << "<a href=\"../index.html\">BACK</a></div>" << std::endl;
            out << "</body>" << std::endl;

            time_t rawtime;
            struct tm* timeinfo;
            time(&rawtime);
            timeinfo = localtime(&rawtime);
            out << "<footer><div align=\"right\"><h6>Team Equin0x</h6><h6>Generated on: " << asctime(timeinfo) << "</h6></div></footer>" << std::endl;
            out << "</html>";
            out.close();
        }

        std::cout << "Writing index.html" << std::endl;
        int o = str.size();

        std::ofstream index("index.html", std::ios::out);

        index << "<html>" << std::endl;
        index << "<body>" << std::endl;
        index << "<ul>" << std::endl;
        index << "<h3>Classrooms</h3>" << std::endl;
        for(int k = 0; k < o; k++){
            index << "<li><a href=\"" << str[k].c_str() << "\">" << str2[k].c_str() << "</a></li>" << std::endl;
        }
        index << "</ul>" << std::endl;

        index << "</body>" << std::endl;
        time_t rawtime;
        struct tm* timeinfo;
        time(&rawtime);
        timeinfo = localtime(&rawtime);
        index << "<footer><div align=\"right\"><h6>Team Equin0x</h6><h6>Generated on: " << asctime(timeinfo) << "</h6></div></footer>" << std::endl;
        index << "</html>";
        index.close();

        //cleanup
        for(int x = 0; x < classroomSize; x++)
            delete[] flags[x];
        delete[] flags;
    }
}


#endif // HTMLGENERATOR_H_INCLUDED
